package com.itmk.web.patrol.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.patrol.entity.PatrolTask;
import com.itmk.web.patrol.service.PatrolTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/patrol/task")
public class PatrolTaskController {

    @Autowired
    private PatrolTaskService patrolTaskService;

    @GetMapping("/page")
    public ResultVo<IPage<PatrolTask>> getPageList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) String taskContent,
            @RequestParam(required = false) String status) {

        Page<PatrolTask> pageParam = new Page<>(page, limit);
        IPage<PatrolTask> pageList = patrolTaskService.getPageList(pageParam, taskContent, status);
        return ResultUtils.success("查询成功",pageList);
    }

    @GetMapping("/{id}")
    public ResultVo<PatrolTask> getById(@PathVariable Long id) {
        PatrolTask patrolTask = patrolTaskService.getById(id);
        return ResultUtils.success(String.valueOf(patrolTask));
    }

    @PostMapping
    public ResultVo<Boolean> save(@RequestBody PatrolTask patrolTask) {
        boolean result = patrolTaskService.save(patrolTask);
        return ResultUtils.success(String.valueOf(result));
    }

    @PutMapping
    public ResultVo<Boolean> update(@RequestBody PatrolTask patrolTask) {
        boolean result = patrolTaskService.updateById(patrolTask);
        return ResultUtils.success(String.valueOf(result));
    }

    @DeleteMapping("/{id}")
    public ResultVo<Boolean> delete(@PathVariable Long id) {
        boolean result = patrolTaskService.removeById(id);
        return ResultUtils.success(String.valueOf(result));
    }

    @PutMapping("/status/{id}/{status}")
    public ResultVo<Boolean> updateStatus(@PathVariable Long id, @PathVariable String status) {
        boolean result = patrolTaskService.updateStatus(id, status);
        return ResultUtils.success(String.valueOf(result));
    }
}